package com.faithbj.dao.impl;

import java.util.List;
import java.util.Map;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.CriteriaSpecification;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.faithbj.common.dao.impl.BaseDaoImpl;
import com.faithbj.common.model.CheckTotal;
import com.faithbj.dao.CheckTotalDao;

@Repository("checkTotalDao")
public class CheckTotalDaoImpl extends BaseDaoImpl<CheckTotal, String>
		implements CheckTotalDao {

	/**
	 * 根据查询条件查询核对总体情况
	 */
	@Override
	public List<CheckTotal> queryCheckTotalListByCondition(Map condition) {
		// TODO Auto-generated method stub
		Session session = this.getSession();
		Criteria criteria = session.createCriteria(CheckTotal.class);
		if (condition.get("id") != null) {
			criteria.add(Restrictions.eq("id", condition.get("id")));
		}
		if (condition.get("regionId") != null) {
			criteria.add(Restrictions.eq("regionId", condition.get("regionId")));
		}
		if (condition.get("acceptStartTime") != null) {
			criteria.add(Restrictions.ge("handleTime",
					condition.get("acceptStartTime")));
		}
		if (condition.get("acceptEndTime") != null) {
			criteria.add(Restrictions.le("handleTime",
					condition.get("acceptEndTime")));
		}
		if (condition.get("mainApplicantName") != null) {
			criteria.add(Restrictions.eq("mainApplicantName",
					condition.get("mainApplicantName")));
		}
		if (condition.get("mainApplicantIdNumber") != null) {
			criteria.add(Restrictions.eq("mainApplicantIdNumber",
					condition.get("mainApplicantIdNumber")));
		}
		criteria.setProjection(null);
		criteria.setResultTransformer(CriteriaSpecification.ROOT_ENTITY);

		return criteria.list();
	}

	@Override
	public List<CheckTotal> queryListFoBatching(boolean batched,
			String[] acceptNums) {
		Criteria criteria = this.getSession().createCriteria(CheckTotal.class);
		criteria.add(Restrictions.eq("batched", batched));
		if (acceptNums != null && acceptNums.length != 0) {
			criteria.add(Restrictions.in("id", acceptNums));
		}
		return criteria.list();
	}

	@Override
    public List<CheckTotal> queryListFoBatching(boolean batched) {
	       Criteria criteria = this.getSession().createCriteria(CheckTotal.class);
	        criteria.add(Restrictions.eq("batched", batched));
	        return criteria.list();
	}

    @Override
	public String queryMaxBatchNumByRegionId(String regionId) {
		String hql = "select max(ct.batchNum) from CheckTotal as ct "
				+ "where ct.regionId=:regionId";
		return (String) getSession().createQuery(hql)
				.setParameter("regionId", regionId).uniqueResult();
	}

	@Override
	public List<CheckTotal> getCheckTotalListForApplyInfoByPage(int from,
			int size) {
		Session session = this.getSession();
		return session.createCriteria(CheckTotal.class).setFirstResult(from)
				.setFetchSize(size).list();
	}
}
